package com.vanvalt.payhui.modules.pay.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.vanvalt.payhui.modules.pay.entity.Pos;
import com.vanvalt.payhui.modules.pay.entity.PosApply;
import com.vanvalt.payhui.modules.pay.service.PosApplyService;
import com.vanvalt.payhui.modules.pay.service.PosService;
import com.vanvalt.payhui.util.constat.Constant;

/**
 * POS机申请
 * @author Xiaoyang
 *
 */
@Controller
@RequestMapping(value = "${adminPath}/pay/pos/apply")
public class PosApplyController extends BaseController {

	@Autowired
	private PosService posService;
	@Autowired
	private PosApplyService posApplyService;
	
	@ModelAttribute
	public PosApply get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return posApplyService.get(id);
		}else{
			return new PosApply();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(PosApply posApply, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<PosApply> page = posApplyService.findPage(new Page<PosApply>(request, response), posApply); 
        model.addAttribute("page", page);
		return "modules/pay/posApplyList";
	}
	
	@RequestMapping(value = "form")
	public String form(PosApply posApply, Model model) {
		
		// POS机产品列表
		Pos pos = new Pos();
		pos.setDelFlag(Constant.DEL_FLAG_NORMAL);
		List<Pos> posList = posService.findList(pos);
		
		model.addAttribute("posList", posList);
		model.addAttribute("posApply", posApply);
		
		return "modules/pay/posApplyForm";
	}
	
	@RequestMapping(value = "save")
	public String save(PosApply posApply, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, posApply)){
			return form(posApply, model);
		}
		
		// 保存提交者用户ID。若为空，则使用当前登录用户ID。
		String userId = posApply.getUserId();
		if(userId == null || "".equals(userId)){
			User user = UserUtils.getUser();
			if(user != null){
				posApply.setUserId(user.getId());
			}
		}
		
		posApplyService.save(posApply);
		addMessage(redirectAttributes, "保存POS申请信息'" + StringUtils.abbr(posApply.getName(),50) + "'成功");
		return "redirect:" + adminPath + "/pay/pos/apply/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(PosApply posApply, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
		
		posApplyService.delete(posApply);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"发布":"删除")+"POS申请信息成功");
		return "redirect:" + adminPath + "/pay/pos/apply/?repage";
	}
	
}
